package metaphor;

import ingest.wiktionary.dal.WiktionaryDB;
import ingest.wiktionary.types.WiktionaryEntry;

import java.util.ArrayList;
import java.util.List;

public class AlternateFormCreator {

	private List<String> objectPronouns = new ArrayList<String>();
	private List<String> possessives = new ArrayList<String>();

	public List<String> createAlternateForms(String phrase) {
		List<String> forms = new ArrayList<String>();
		String[] wordArray = phrase.split(" ");
		ArrayList<String> words = new ArrayList<String>();
		for (String w : wordArray) {
			words.add(w);
		}
		for (int i = 0; i < words.size(); i++) {
			String word = words.get(i);

			// generate alt pronoun forms
			if (objectPronouns.contains(word)) {
				forms.addAll(generateAlternatives(words, i, objectPronouns));
			}
			if (possessives.contains(word)) {
				forms.addAll(generateAlternatives(words, i, possessives));

			}
		}
		return forms;
	}

	private List<String> generateAlternatives(List<String> words,
			int replaceIndex, List<String> replacementWords) {
		List<String> forms = new ArrayList<String>();
		for (String replacementWord : replacementWords) {
			words.set(replaceIndex, replacementWord);
			forms.add(list2String(words));
		}
		return forms;
	}

	private String list2String(List<String> words) {
		StringBuilder sb = new StringBuilder();
		for (String word : words) {
			sb.append(word + " ");
		}
		return sb.toString();
	}

	private void setup() {
		// make the list of pronouns
		objectPronouns.add("anybody");
		objectPronouns.add("anything");
		objectPronouns.add("each of them");
		objectPronouns.add("everybody");
		objectPronouns.add("everyone");
		objectPronouns.add("everything");
		objectPronouns.add("herself");
		objectPronouns.add("himself");
		objectPronouns.add("him");
		objectPronouns.add("her");
		objectPronouns.add("itself");
		objectPronouns.add("themselves");
		objectPronouns.add("myself");
		objectPronouns.add("myself");
		objectPronouns.add("one another");
		objectPronouns.add("ourselves");
		objectPronouns.add("oneself");
		objectPronouns.add("somebody");
		objectPronouns.add("something");
		objectPronouns.add("themself");
		objectPronouns.add("themselves");
		objectPronouns.add("whoever");
		objectPronouns.add("whichever");
		objectPronouns.add("whomever");
		objectPronouns.add("yourself");
		objectPronouns.add("yourselves");

		// make the list of posessives
		possessives.add("my");
		possessives.add("your");
		possessives.add("his");
		possessives.add("her");
		possessives.add("its");
		possessives.add("somebody's");
		possessives.add("someone's");
		possessives.add("our");
		possessives.add("your");
		possessives.add("one's");
		possessives.add("their");

	}

	public AlternateFormCreator() {
		setup();
	}

	
	
	public static void main(String[] args) {
		WiktionaryDB db = WiktionaryDB.getInstance();
		List<WiktionaryEntry> entries = db.getAllEntries();
		AlternateFormCreator f = new AlternateFormCreator();

		for (WiktionaryEntry e : entries) {
			String idiom = e.getIdiom();
			System.out.println(e.getIdiom().toLowerCase());
			List<String> forms = f.createAlternateForms(idiom);
			for (String s : forms){
			db.update(e, "alternativeForms", s);
			}
		}
	}
}
